package com.hrt.datasender.process.strategy.hj212;

import com.btcode.db.result.Row;
import com.btcode.db.sqlparameter.SQLParameterList;
import com.btcode.orm.conver.DefaultModelConver;
import com.btcode.schedule.ScheduleManager;
import com.hrt.datasender.datacollect.DataGetter;
import com.hrt.datasender.datacollect.drive.weigeng.BdDoorAccessModel;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.datasender.datadao.service.BCSiteService;
import com.hrt.datasender.mq.MqKit;
import com.hrt.datasender.mq.model.PacketModel;
import com.hrt.datasender.process.indentity.hj212.MessageModel;
import com.hrt.datasender.process.strategy.IProcessStrategy;
import com.hrt.datasender.process.strategy.hj212.util.CRCUtil;
import com.hrt.datasender.process.strategy.hj212.util.DataType;
import com.hrt.datasender.process.strategy.hj212.util.StrategyUtil;
import com.hrt.db.DBManager;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class Com3020Process extends ProcessStrategyMonitorData {
    
    @Override
    public String getStrategyName() {
        return "设备运行信息解析";
    }

    @Override
    public DataType getDataType() {
        return DataType.deviceOper;
    }

    @Override
    public String getCommandId() {
        return "3020";
    }

    @Override
    public String getPacketCpStr(BcSendPlatformModel platform, List<Row> rowList, DataType dataType) {

        DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

        if (rowList.size() < 1) {
            return null;
        }

        long date = (long) rowList.get(0).get("DATATIME");

        StringBuffer sb = new StringBuffer("DataTime=" + sdf.format(new Date(date)) + ";");
        for (Row row : rowList) {
            String pramCodeBus = String.valueOf(row.get("PARAM_CODE_BUS"));
            if("B01TOTAL".equals(pramCodeBus)){
                continue;
            }
            List<String> deviceParams = new ArrayList<>();
            deviceParams.add("i12001");
            deviceParams.add("i13001");
            deviceParams.add("i13002");
            deviceParams.add("i13003");
            deviceParams.add("i13004");
            deviceParams.add("i13005");
            deviceParams.add("i13006");
            deviceParams.add("i13007");
            deviceParams.add("i13008");
            deviceParams.add("i13009");
            deviceParams.add("i13010");


            for(String deviceParam:deviceParams){
                String value = String.valueOf(row.get(deviceParam.toUpperCase()));
                if(!"".equals(value)){
                    sb.append("PolId=" + pramCodeBus + ";");
                    break;
                }
            }
            for(String deviceParam:deviceParams){
                String value = String.valueOf(row.get(deviceParam.toUpperCase()));
                value = value.replace("&","/");
                if(!"".equals(value)){
                    sb.append(deviceParam + "-Info=" + value + ";");
                }
            }
        }

        sb.setLength(sb.length() - 1);
        return sb.toString();
    }

    /**
     * 响应平台指令
     * @param data
     * @param sendPlatform
     */
    @Override
    public void decode(MessageModel data, BcSendPlatformModel sendPlatform) {
        String msg = data.getMsg() + "";
        /**
         * 返回请求应答
         */
        StrategyUtil.responseMsg(sendPlatform,msg,"0","");

        /**
         * ①收到报文，解析报文，插入到组态数据表中
         * ②开启定期扫描功能，扫描组态数据表，若扫到组态数据表有执行结果数据。
         * 判断执行结果是否正常，正常则转发响应报文并返回成功，错误返回失败。
         * （设置过期时间，比如30秒还没完成，要把任务关闭）
         */
        String id = StrategyUtil.insertRcCmd(msg);

        /**
         * 考虑到access不支持多线程的问题，暂时模拟发送回成功执行
         */
        String response = "QN=20200310145000223;ST=32;CN=3020;PW=123456;MN=D0300035978960;" +
                "Flag=4;CP=&&DataTime=20200311150000;PolId=011;i12001-Info=0;i13001-Info=0/500;" +
                "i13006-Info=20200210080500;i13007-Info=0.6;i13008-Info=1.2&&";
        Object packetMsg = StrategyUtil.getCompleteMsg(response);
        PacketModel packetModel = new PacketModel();
        packetModel.setIp(sendPlatform.getIp());
        packetModel.setPort(sendPlatform.getPort());
        packetModel.setMn(sendPlatform.getMn());
        packetModel.setMsg(packetMsg);
        MqKit.productMsgToRecriminateMQ(packetModel);
        StrategyUtil.responseMsg(sendPlatform,msg,"1","1");

//        List<Row> rows = StrategyUtil.readRcResult(id,60*1000);
//
//        if(rows!=null&&rows.size()>0){
//            String result = rows.get(0).getSafeString("CMD_RESULT");
//            if(result.contains("ExeRtn=1")){
//                /**
//                 * 把response报文转发
//                 * 返回执行成功结果
//                 */
//                String response = rows.get(0).getSafeString("CMD_RESPONSE");
//                Object packetMsg = StrategyUtil.getCompleteMsg(response);
//                PacketModel packetModel = new PacketModel();
//                packetModel.setIp(sendPlatform.getIp());
//                packetModel.setPort(sendPlatform.getPort());
//                packetModel.setMn(sendPlatform.getMn());
//                packetModel.setMsg(packetMsg);
//                MqKit.productMsgToRecriminateMQ(packetModel);
//                StrategyUtil.responseMsg(sendPlatform,msg,"1","1");
//            }
//            else{
//                /**
//                 * 返回执行失败结果
//                 */
//                StrategyUtil.responseMsg(sendPlatform,msg,"1","2");
//            }
//        }
//        else{
//            /**
//             * 返回执行失败结果
//             */
//            StrategyUtil.responseMsg(sendPlatform,msg,"1","2");
//        }
    }
}
